import { CradColor, mark } from "./enums";
import { Card, NormalCard,Joker } from "./types";

export class Deck{
    cards:Card[] =[];
    constructor(cards?:Card[]){
        if(cards){
            this.cards = cards
        }else{
            this.initDeck()
        }
       
    }
    initDeck(){
    // const deck:Deck = [] 
    const marks = Object.values(mark)
    const colors = Object.values(CradColor)
    for (const m of marks) {
        const element = m;
        for(const c of colors){
            const card:NormalCard={
                color:c,
                mark:m,
                getString(){
                   return this.color+this.mark
                }
                
            }
            this.cards.push(card)
        }
        
    }
    let Joker:Joker = {
        type:"big",
        getString(){
            return "Joker"
        }
    }
    let joker:Joker = {
        type:"small",
        getString(){
            return "joker"
        }
    }
    this.cards.push(Joker)
    this.cards.push(joker)
    return this.cards
    }
    printDeck(){
        let result="\n";
        this.cards.forEach((item,index)=>{
           let  str= item.getString();
            result +=str+"\t"
           if((index+1)%6 ===0){
            result += "\n"
           }
        }) 
        console.log(result)
    }
    // 洗牌
    shuffle(){

          for (let index = 0; index < this.cards.length; index++) {
            const targetIndex = this.getRadom(0,this.cards.length)
            const temp = this.cards[index];
            this.cards[index] = this.cards[targetIndex];
            this.cards[targetIndex] = temp;
          }
    }
    // 发牌
    publish():[Deck,Deck,Deck,Deck]{
        // const result:[Deck,Deck,Deck,Deck] = [[],[],[],[]]
        let player1:Deck,player2:Deck,player3:Deck,player4:Deck;
            player1 = this.takeCards(17)
            player2 = this.takeCards(17)
            player3 = this.takeCards(17)
            player4 = this.takeCards(3)
            // console.log(player1,player2,player3,player4)
        return [player1,player2,player3,player4]
    }
    private takeCards(nums:number):Deck{
              let cards:Card[] = [];
              for (let index = 0; index < nums; index++) {
                cards.push(this.cards.shift() as Card)
              }
              return new Deck(cards)
    }
    private getRadom(min:number,max:number){
        const desc = max-min
        return Math.floor(Math.random()*desc+min)
    }
}